| Conditions | 1 |
| Paths | 2 |
| Total Lines | 209 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | (function (jQuery,chai) { |
||
| 66 | describe('jQuery.Always', function () {
|
||
| 67 | it('Dependencies have resolved', function () {
|
||
| 68 | var mo = typeof window.MutationObserver, wmo = typeof window.WebKitMutationObserver; |
||
| 69 | assert.isTrue('undefined' !== mo || 'undefined' !== wmo, 'Browser must support mutation observer feature,' +
|
||
| 70 | ' typeof MutationObserver is ' + mo + ', typeof WebKitMutationObserver is ' + wmo); |
||
| 71 | }); |
||
| 72 | it('Plugin has registered', function () {
|
||
| 73 | assert.isFunction($.fn.always, 'always() must be registered with jQuery'); |
||
| 74 | assert.isFunction($.fn.never, 'never() must be registered with jQuery'); |
||
| 75 | }); |
||
| 76 | it('"Inserted" callbacks are executed for matching selectors (before registration)', function (done) {
|
||
| 77 | var callbacks = Utils.createCallbacks(); |
||
| 78 | $fixture |
||
| 79 | .append($a) |
||
| 80 | .append($b) |
||
| 81 | .append($div) |
||
| 82 | .always('a, b', Utils.invokeCallbacks(callbacks));
|
||
| 83 | Utils.wait(1, function () {
|
||
| 84 | Utils.assertInvoked(callbacks, 1, 1, 0); |
||
| 85 | done(); |
||
| 86 | }); |
||
| 87 | }); |
||
| 88 | it('"Inserted" callbacks are executed for matching selectors (after registration)', function (done) {
|
||
| 89 | var callbacks = Utils.createCallbacks(); |
||
| 90 | $fixture |
||
| 91 | .always('a, b', Utils.invokeCallbacks(callbacks))
|
||
| 92 | .append($a) |
||
| 93 | .append($b) |
||
| 94 | .append($div); |
||
| 95 | Utils.wait(1, function () {
|
||
| 96 | Utils.assertInvoked(callbacks, 1, 1, 0); |
||
| 97 | done(); |
||
| 98 | }); |
||
| 99 | }); |
||
| 100 | it('"Removed" callbacks are executed for matching selectors', function (done) {
|
||
| 101 | var callbacks = Utils.createCallbacks(); |
||
| 102 | $fixture |
||
| 103 | .append($a) |
||
| 104 | .append($b) |
||
| 105 | .append($div) |
||
| 106 | .always('a, b', undefined, Utils.invokeCallbacks(callbacks));
|
||
| 107 | $a.remove(); |
||
| 108 | $b.remove(); |
||
| 109 | Utils.wait(1, function () {
|
||
| 110 | Utils.assertInvoked(callbacks, 1, 1, 0); |
||
| 111 | done(); |
||
| 112 | }); |
||
| 113 | }); |
||
| 114 | it('Callbacks are also executed for selectors matching children (deep) of mutated' +
|
||
| 115 | ' elements', function (done) {
|
||
| 116 | var insertedCallbacks = Utils.createCallbacks(), removedCallbacks = Utils.createCallbacks(), $aWrapper = $('<span/>'), $bWrapper = $('<span/>'), $divWrapper = $('<span/>');
|
||
| 117 | $aWrapper.append($('<span/>').append($a.clone()).append($('<span/>').append($a.clone())));
|
||
| 118 | $bWrapper.append($('<span/>').append($b.clone()).append($('<span/>').append($b.clone())));
|
||
| 119 | $divWrapper.append($('<span/>').append($div.clone()).append($('<span/>').append($div.clone())));
|
||
| 120 | $fixture |
||
| 121 | .always('a, b', Utils.invokeCallbacks(insertedCallbacks), Utils.invokeCallbacks(removedCallbacks))
|
||
| 122 | .append($aWrapper) |
||
| 123 | .append($bWrapper) |
||
| 124 | .append($divWrapper); |
||
| 125 | Utils.wait(1, function () {
|
||
| 126 | Utils.assertInvoked(insertedCallbacks, 2, 2, 0); |
||
| 127 | Utils.assertInvoked(removedCallbacks, 0, 0, 0); |
||
| 128 | $aWrapper.children().children().last().remove(); |
||
| 129 | $bWrapper.remove(); |
||
| 130 | $divWrapper.remove(); |
||
| 131 | Utils.wait(1, function () {
|
||
| 132 | Utils.assertInvoked(insertedCallbacks, 2, 2, 0); |
||
| 133 | Utils.assertInvoked(removedCallbacks, 1, 2, 0); |
||
| 134 | done(); |
||
| 135 | }); |
||
| 136 | }); |
||
| 137 | }); |
||
| 138 | it('Callbacks are not called multiple times for special cases (like wrap() / unwrap()) where child nodes' +
|
||
| 139 | ' are also reported as mutated', function (done) {
|
||
| 140 | var insertedCallbacks = Utils.createCallbacks(), removedCallbacks = Utils.createCallbacks(), $aWrapper = $('<span/>');
|
||
| 141 | $fixture |
||
| 142 | .always('a', Utils.invokeCallbacks(insertedCallbacks), Utils.invokeCallbacks(removedCallbacks))
|
||
| 143 | .append($a); |
||
| 144 | Utils.wait(1, function () {
|
||
| 145 | Utils.assertInvoked(insertedCallbacks, 1, 0, 0); |
||
| 146 | Utils.assertInvoked(removedCallbacks, 0, 0, 0); |
||
| 147 | $a.wrap($aWrapper); |
||
| 148 | Utils.wait(1, function () {
|
||
| 149 | Utils.assertInvoked(insertedCallbacks, 2, 0, 0); |
||
| 150 | Utils.assertInvoked(removedCallbacks, 1, 0, 0); |
||
| 151 | $a.unwrap(); |
||
| 152 | Utils.wait(1, function () {
|
||
| 153 | Utils.assertInvoked(insertedCallbacks, 3, 0, 0); |
||
| 154 | Utils.assertInvoked(removedCallbacks, 2, 0, 0); |
||
| 155 | done(); |
||
| 156 | }); |
||
| 157 | }); |
||
| 158 | }); |
||
| 159 | }); |
||
| 160 | it('Calling never() stops execution of all callbacks', function (done) {
|
||
| 161 | var insertedCallbacks = Utils.createCallbacks(), removedCallbacks = Utils.createCallbacks(); |
||
| 162 | $fixture |
||
| 163 | .always('a, b', Utils.invokeCallbacks(insertedCallbacks), Utils.invokeCallbacks(removedCallbacks))
|
||
| 164 | .append($a) |
||
| 165 | .append($b) |
||
| 166 | .append($div); |
||
| 167 | Utils.wait(1, function () {
|
||
| 168 | Utils.assertInvoked(insertedCallbacks, 1, 1, 0, 'Pre-Never (Inserted)'); |
||
| 169 | $a.remove(); |
||
| 170 | $b.remove(); |
||
| 171 | $div.remove(); |
||
| 172 | Utils.wait(1, function () {
|
||
| 173 | Utils.assertInvoked(removedCallbacks, 1, 1, 0, 'Pre-Never (Removed)'); |
||
| 174 | $fixture |
||
| 175 | .never() |
||
| 176 | .append($a) |
||
| 177 | .append($b) |
||
| 178 | .append($div); |
||
| 179 | Utils.wait(1, function () {
|
||
| 180 | Utils.assertInvoked(insertedCallbacks, 1, 1, 0, 'Post-Never (Inserted)'); |
||
| 181 | $a.remove(); |
||
| 182 | $b.remove(); |
||
| 183 | $div.remove(); |
||
| 184 | Utils.wait(1, function () {
|
||
| 185 | Utils.assertInvoked(removedCallbacks, 1, 1, 0, 'Post-Never (Removed)'); |
||
| 186 | done(); |
||
| 187 | }); |
||
| 188 | }); |
||
| 189 | }); |
||
| 190 | }); |
||
| 191 | }); |
||
| 192 | it('Calling never(selector) stops execution of matching callbacks', function (done) {
|
||
| 193 | var callbacksAB = Utils.createCallbacks(), callbacksDIV = Utils.createCallbacks(); |
||
| 194 | $fixture |
||
| 195 | .always('a, b', Utils.invokeCallbacks(callbacksAB))
|
||
| 196 | .always('div', Utils.invokeCallbacks(callbacksDIV))
|
||
| 197 | .append($a) |
||
| 198 | .append($b) |
||
| 199 | .append($div); |
||
| 200 | Utils.wait(1, function () {
|
||
| 201 | Utils.assertInvoked(callbacksAB, 1, 1, 0, 'Pre-Never (A,B)'); |
||
| 202 | Utils.assertInvoked(callbacksDIV, 0, 0, 1, 'Pre-Never (DIV)'); |
||
| 203 | $a.remove(); |
||
| 204 | $b.remove(); |
||
| 205 | $div.remove(); |
||
| 206 | $fixture |
||
| 207 | .never('b,a')
|
||
| 208 | .append($a) |
||
| 209 | .append($b) |
||
| 210 | .append($div); |
||
| 211 | Utils.wait(1, function () {
|
||
| 212 | Utils.assertInvoked(callbacksAB, 1, 1, 0, 'Post-Never (A,B)'); |
||
| 213 | Utils.assertInvoked(callbacksDIV, 0, 0, 2, 'Post-Never (DIV)'); |
||
| 214 | done(); |
||
| 215 | }); |
||
| 216 | }); |
||
| 217 | }); |
||
| 218 | it('Calling never(selector, insertedCallback, removedCallback) stops execution of exact callback(s)', function (done) {
|
||
| 219 | var callbacksInsertedA = Utils.createCallbacks(), callbacksInsertedB = Utils.createCallbacks(), callbacksRemovedDIV = Utils.createCallbacks(), _callbacksInsertedA = Utils.invokeCallbacks(callbacksInsertedA), _callbacksInsertedB = Utils.invokeCallbacks(callbacksInsertedB), _callbacksRemovedDIV = Utils.invokeCallbacks(callbacksRemovedDIV); |
||
| 220 | $fixture |
||
| 221 | .always('a', _callbacksInsertedA)
|
||
| 222 | .always('b', _callbacksInsertedB)
|
||
| 223 | .always('div', undefined, _callbacksRemovedDIV)
|
||
| 224 | .append($a) |
||
| 225 | .append($b) |
||
| 226 | .append($div); |
||
| 227 | $div.remove(); |
||
| 228 | Utils.wait(1, function () {
|
||
| 229 | Utils.assertInvoked(callbacksInsertedA, 1, 0, 0, 'Pre-Never (A)'); |
||
| 230 | Utils.assertInvoked(callbacksInsertedB, 0, 1, 0, 'Pre-Never (B)'); |
||
| 231 | Utils.assertInvoked(callbacksRemovedDIV, 0, 0, 1, 'Pre-Never (DIV)'); |
||
| 232 | $a.remove(); |
||
| 233 | $b.remove(); |
||
| 234 | $fixture |
||
| 235 | .never('a', _callbacksInsertedA)
|
||
| 236 | .append($a) |
||
| 237 | .append($b) |
||
| 238 | .append($div); |
||
| 239 | $div.remove(); |
||
| 240 | Utils.wait(1, function () {
|
||
| 241 | Utils.assertInvoked(callbacksInsertedA, 1, 0, 0, 'Post-Never (A)'); |
||
| 242 | Utils.assertInvoked(callbacksInsertedB, 0, 2, 0, 'Post-Never (B)'); |
||
| 243 | Utils.assertInvoked(callbacksRemovedDIV, 0, 0, 2, 'Post-Never (DIV)'); |
||
| 244 | $a.remove(); |
||
| 245 | $b.remove(); |
||
| 246 | $fixture |
||
| 247 | .never('div', undefined, _callbacksRemovedDIV)
|
||
| 248 | .append($a) |
||
| 249 | .append($b) |
||
| 250 | .append($div); |
||
| 251 | $div.remove(); |
||
| 252 | Utils.wait(1, function () {
|
||
| 253 | Utils.assertInvoked(callbacksInsertedA, 1, 0, 0, 'Post-Never (A)'); |
||
| 254 | Utils.assertInvoked(callbacksInsertedB, 0, 3, 0, 'Post-Never (B)'); |
||
| 255 | Utils.assertInvoked(callbacksRemovedDIV, 0, 0, 2, 'Post-Never (DIV)'); |
||
| 256 | done(); |
||
| 257 | }); |
||
| 258 | }); |
||
| 259 | }); |
||
| 260 | }); |
||
| 261 | it('Adding a second "inserted" callback eligible for immediate execution does not trigger re-execution of the first one', function (done) {
|
||
| 262 | var callbacks = Utils.createCallbacks(); |
||
| 263 | $fixture |
||
| 264 | .append($a.attr('data-ab', ''))
|
||
| 265 | .append($b.attr('data-ab', ''))
|
||
| 266 | .append($div) |
||
| 267 | .always('a', Utils.invokeCallbacks(callbacks))
|
||
| 268 | .always('[data-ab]', Utils.invokeCallbacks(callbacks));
|
||
| 269 | Utils.wait(1, function () {
|
||
| 270 | Utils.assertInvoked(callbacks, 2, 1, 0); |
||
| 271 | done(); |
||
| 272 | }); |
||
| 273 | }); |
||
| 274 | }); |
||
| 275 | })(jQuery, chai.assert); |
||
| 278 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.